//
//  MMerchantVipCardViewController.m
//  NeusoftHBSJSober
//
//  Created by Apple-21 on 15/11/9.
//  Copyright (c) 2015年 com.neusoftsober. All rights reserved.
//

#import "MMerchantVipCardViewController.h"
#import "MHandleCardViewController.h"
#import "MVIPCardService.h"
#import "MDDataBaseUtil.h"
#import "MMerchantModel.h"
#import "MListModel.h"
#import "MMerchantViewController.h"
#import "MDetailMerchantModel.h"
#import "VIPCardReturnCashModel.h"
#import "MEnjoyViewController.h"
@interface MMerchantVipCardViewController ()<UITableViewDelegate, UITableViewDataSource> {

    MHandleCardViewController *handleCardVC;
    MMerchantViewController *merchantVC;
    MEnjoyViewController *enjoyVC;
}

- (IBAction)backAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *merchantDetailBtn;
- (IBAction)merchantDetailBtnAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UILabel *salemenName;
@property (weak, nonatomic) IBOutlet UILabel *salemenAddress;
@property (weak, nonatomic) IBOutlet UILabel *salemenPhone;
@property (weak, nonatomic) IBOutlet UIButton *returnCashBtn;
- (IBAction)returnCashAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *clearCalulateBtn;

- (IBAction)clearCalulateAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *enjoyBtn;
- (IBAction)enjoyBtnAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (weak, nonatomic) IBOutlet UILabel *pickupCashLabel;
@property (weak, nonatomic) IBOutlet UILabel *hasReturnCashLabel;
@property (weak, nonatomic) IBOutlet UILabel *returnCashLabel;
@property (weak, nonatomic) IBOutlet UILabel *returnPercentStringLabel;

@property (nonatomic, strong) MListModel *listModel;

@property (nonatomic, strong) NSMutableArray *myVipCardReturnMutArray;
@end

@implementation MMerchantVipCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _initButton];
    
   
}
- (void)_initButton {

    [self.merchantDetailBtn.layer setCornerRadius:3.0];
    [self.merchantDetailBtn.layer setMasksToBounds:YES];
    [self.merchantDetailBtn.layer setBorderColor:[UIColor blackColor].CGColor];
    [self.merchantDetailBtn.layer setBorderWidth:1.0];
    [self.returnCashBtn.layer setCornerRadius:3.0];
    [self.returnCashBtn.layer setMasksToBounds:YES];
    [self.returnCashBtn.layer setBorderColor:[UIColor blackColor].CGColor];
    [self.returnCashBtn.layer setBorderWidth:1.0];
    [self.clearCalulateBtn.layer setCornerRadius:3.0];
    [self.clearCalulateBtn.layer setMasksToBounds:YES];
    [self.clearCalulateBtn.layer setBorderColor:[UIColor blackColor].CGColor];
    [self.clearCalulateBtn.layer setBorderWidth:1.0];
    [self.enjoyBtn.layer setCornerRadius:3.0];
    [self.enjoyBtn.layer setMasksToBounds:YES];
    [self.enjoyBtn.layer setBorderColor:[UIColor blackColor].CGColor];
    [self.enjoyBtn.layer setBorderWidth:1.0];
    
}
- (void)viewWillAppear:(BOOL)animated {

    [self getSalemenInfomation];
    [self.myTableView setSeparatorInset:UIEdgeInsetsZero];
    if (self.myVipCardModel) {
        self.salemenName.text = self.myVipCardModel.salemanName;
        self.salemenAddress.text = self.myVipCardModel.salemanAddress;
        self.salemenPhone.text = self.myVipCardModel.salemanPhone;
        self.hasReturnCashLabel.text = [NSString stringWithFormat:@"%@元",self.myVipCardModel.hasReturnCash];
        self.returnCashLabel.text = self.myVipCardModel.returnCash;
        self.pickupCashLabel.text = [NSString stringWithFormat:@"%@元",self.myVipCardModel.pickUpCash];
        self.returnPercentStringLabel.text = [NSString stringWithFormat:@"%@折",self.myVipCardModel.returnPercentString];
        
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.myVipCardReturnMutArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellStr = @"cellIndentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
        
        UILabel *moneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH/3-20, 28)];
        moneyLabel.textAlignment = NSTextAlignmentCenter;
        moneyLabel.font = [UIFont fontWithName:nil size:12.0];
        moneyLabel.tag = 101;
        [cell.contentView addSubview:moneyLabel];
        
        UILabel *useTimeLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREENWIDTH/3-20, 0, SCREENWIDTH/3+20, 28)];
        useTimeLabel.textAlignment = NSTextAlignmentCenter;
        useTimeLabel.font = [UIFont fontWithName:nil size:12.0];
        useTimeLabel.tag = 102;
        [cell.contentView addSubview:useTimeLabel];
        
        UILabel *userLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREENWIDTH/3*2, 0, SCREENWIDTH/3, 28)];
        userLabel.textAlignment = NSTextAlignmentCenter;
        userLabel.font = [UIFont fontWithName:nil size:12.0];
        userLabel.tag = 103;
        [cell.contentView addSubview:userLabel];
    }
    VIPCardReturnCashModel *model = [[VIPCardReturnCashModel alloc]init];
    model = [self.myVipCardReturnMutArray objectAtIndex:indexPath.row];
    ((UILabel *)[cell.contentView viewWithTag:101]).text = model.money;
    NSString *string1 = [model.createTime substringToIndex:10];
    NSString *string2 = [model.createTime substringFromIndex:10];
    ((UILabel *)[cell.contentView viewWithTag:102]).text = [NSString stringWithFormat:@"%@ %@",string1,string2];
    ((UILabel *)[cell.contentView viewWithTag:103]).text = model.userUserName;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)backAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)merchantDetailBtnAction:(UIButton *)sender {
    
    merchantVC = [[MMerchantViewController alloc]init];
    merchantVC.model = self.listModel;
    merchantVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:merchantVC animated:YES completion:nil];
}
- (IBAction)returnCashAction:(UIButton *)sender {
    
    handleCardVC = [[MHandleCardViewController alloc]init];
    handleCardVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:handleCardVC animated:YES completion:nil];
}
- (IBAction)clearCalulateAction:(UIButton *)sender {
}
- (IBAction)enjoyBtnAction:(UIButton *)sender {
    
    enjoyVC = [[MEnjoyViewController alloc]init];
    enjoyVC.myVipCardModel = self.myVipCardModel;
    enjoyVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentViewController:enjoyVC animated:YES completion:nil];
}
- (void)getSalemenInfomation {
    
    [[MVIPCardService sharedClient]getSalemanInfoWithUserId:[self.myVipCardModel.salemanUserID integerValue] completion:^(MMerchantModel *model){

        self.listModel = [[MListModel alloc]init];
        self.listModel.companyName = model.companyName;
        self.listModel.phone = model.phone;
        self.listModel.address = model.companyAddress;
        self.listModel.latitude = model.positionX;
        self.listModel.longtitude = model.positionY;
        self.listModel.saleManMemo = model.memo;
        self.listModel.createUserID = model.ID;
        
        [[MVIPCardService sharedClient]getMerchantInfoWithMerchantID:self.myVipCardModel.salemanUserID latitude:[MDDataBaseUtil txtLatitude] longtitude:[MDDataBaseUtil txtLongtitude] completion:^(MDetailMerchantModel *model){
        
            self.listModel.distance = model.radius;
        } failure:^(NSString *error){
        }];
        
    } failure:^(NSString *error){
    }];
    [[MVIPCardService sharedClient] getVipCardComsumeListWithCardID:[self.myVipCardModel.ID integerValue] pageIndex:1 pageSize:20 userID:[[MDDataBaseUtil userID]integerValue] sortType:@"CreateTime|1" userName:nil completion:^(NSMutableArray *result){
        self.myVipCardReturnMutArray = [NSMutableArray arrayWithArray:result];
        [self.myTableView reloadData];
        
    } failure:^(NSString *error){
        
        
    }];

    
    
}
@end
